package com.zhouyibike.uu.ui.activity.yihoudong;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.zhouyibike.uu.R;
import com.zhouyibike.uu.config.Config;
import com.zhouyibike.uu.entity.GetCodeResult;
import com.zhouyibike.uu.entity.MyYiQuanTransferResult;
import com.zhouyibike.uu.http.ApiCallback;
import com.zhouyibike.uu.ui.activity.web.TotalWebActivity;
import com.zhouyibike.uu.ui.base.BaseActivity;
import com.zhouyibike.uu.utils.TextUtils;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;

public class YihuodongzhuanrangActivity extends BaseActivity {
    private int cansellnum = 0;
    private int sellingnum = 0;
    private Button quedingbt;
    private TextView jiatv, jiantv, canselltv, sellingtv,zhouyidanchehuodongxieyi;
    private EditText sellingedit;
    private ImageButton mBtnBack;
    private TextView mBtnTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yihuodongzhuanrang);
        cansellnum = getIntent().getIntExtra("cansellnum", 0);
        initView();
        initListener();
    }

    private void initListener() {
        zhouyidanchehuodongxieyi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getWZMsg();
            }
        });
        mBtnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        jiatv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sellingnum += 1;
                sellingedit.setText(sellingnum + "");
            }
        });
        jiantv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sellingnum -= 1;
                sellingedit.setText(sellingnum + "");
            }
        });
        sellingedit.addTextChangedListener(new EditTextWatcher());
        quedingbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isNullorEmpty(sellingedit.getText().toString())) {
                    toastShow("请输入转让翼券数量");
                } else {
                    if (cansellnum >= sellingnum) {
                        zhuanrang();
                    } else {
                        toastShow("可转让翼券数量不足");
                    }
                }
            }
        });
    }

    private void initView() {
        mBtnBack = (ImageButton) findViewById(R.id.bar_btn_back);
        mBtnTitle = (TextView) findViewById(R.id.bar_text_title);
        quedingbt = (Button) findViewById(R.id.yiquanzhuanrang_submit);
        jiatv = (TextView) findViewById(R.id.yiquangongzhuanrangnumtvjia);
        jiantv = (TextView) findViewById(R.id.yiquangongzhuanrangnumtvjian);
        canselltv = (TextView) findViewById(R.id.yiquankezhuanrangnumtv);
        sellingedit = (EditText) findViewById(R.id.zhuanrangedittext);
        sellingtv = (TextView) findViewById(R.id.yiquangongzhuanrangnumtv);
        canselltv.setText(cansellnum + "张");
        mBtnTitle.setText("翼券转让");
        jiantv.setEnabled(false);
        zhouyidanchehuodongxieyi = (TextView) findViewById(R.id.zhouyidanchehuodongxieyi);

    }

    class EditTextWatcher implements TextWatcher {

        public void afterTextChanged(Editable edt) {
            if (TextUtils.isNullorEmpty(sellingedit.getText().toString())) {
                jiantv.setEnabled(false);
                jiatv.setEnabled(false);
                sellingnum = 0;
                sellingtv.setText(sellingnum + "张");
            } else {
                jiatv.setEnabled(true);
                if (Integer.parseInt(sellingedit.getText().toString()) == 0) {
                    jiantv.setEnabled(false);
                } else {
                    jiantv.setEnabled(true);
                }
                sellingnum = Integer.parseInt(sellingedit.getText().toString());
                sellingtv.setText(sellingnum + "张");
            }
            sellingedit.setSelection(edt.length());//将光标移至文字末尾
        }

        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }

        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }

    }

    /**
     * //进行转让
     */
    private void zhuanrang() {
        Map<String, String> params = new HashMap<>();
        params.put("token", preferences.getString("token", ""));
        params.put("count", sellingnum + "");
        showProgressDialog();
        addSubscription(apiStores.MyYiQuanTransfer(params),
                new ApiCallback<MyYiQuanTransferResult>() {

                    @Override
                    public void onSuccess(MyYiQuanTransferResult model) {
                        if (model.getStatus() == Config.SUCCESSCODE) {
                            toastShow(model.getMessage());
                            setResult(1);
                            finish();
                        } else {
                            toastShow(model.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);

                    }

                    @Override
                    public void onFinish() {
                        dismissProgressDialog();
                        EventBus.getDefault().post("yiquan3sellok");
                    }
                });
    }
    private void getWZMsg(){
        Map<String, String> params = new HashMap<>();
        params.put("token", preferences.getString("token",""));
        params.put("type", "7");
        showProgressDialog();
        addSubscription(apiStores.GetInformationzcxy(params),
                new ApiCallback<GetCodeResult>() {

                    @Override
                    public void onSuccess(GetCodeResult model) {
                        if(model.getStatus()== Config.SUCCESSCODE){
                            Intent i = new Intent(YihuodongzhuanrangActivity.this, TotalWebActivity.class);
                            i.putExtra("title","翼券服务协议");
                            i.putExtra("url",model.getData());
                            startActivity(i);
                        }else{
                            toastShow(model.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);
                    }

                    @Override
                    public void onFinish() {
                        dismissProgressDialog();
                    }
                });
    }
}
